<?php

namespace App\Controller\RESTFul;

use App\Controller\AbstractController;
use Yolo\Service\Annotations\RESTFul;
use Yolo\Service\ApiGateway;
use Yolo\Service\Enums\HttpMethod;

/**
 * IndexController
 * @author lushaoming<lushaoming6@gmail.com>
 * @date 2024/1/27
 */
class IndexController extends AbstractController
{

    #[RESTFul(HttpMethod::GET, '/yolo/test1', [
        // RESTFul参数校验分三类
        'validation' => [
            'qs' => [],
            'path' => [],
            'body' => []
        ],
        'options' => [
            'login' => 'none', // none - 无需登录，optional - 可选登录，required - 强制登录
        ]
    ])]
    public function testRestful(array $args): array
    {
        $qs = $args['qs']; // qs参数
        $pathArgs = $args['path'] ?? null; // 路径上的参数，如果没有，则为null
        return ApiGateway::OK_RESPONSE;
    }

    #[RESTFul(HttpMethod::GET, '/yolo/test/{name}', [
        'validation' => [
            'path' => [
                'name' => 'required|numeric'
            ],
            'qs' => [
                'a' => 'required'
            ]
        ],
        'options' => [
            'login' => 'required',
            'clientIp' => true, // 需要客户端ip,
            'cookie' => true,
        ]
    ])]
    public function testRestfulWithPathArgs(array $args, array $metadata): array
    {
//        throw RPC::error('test_error', [
//            'a' => $args['qs']['a'],
//            'name' => $args['path']['name']
//        ]);
//        $qs = $args['qs']; // qs参数
//        $pathArgs = $args['path'] ?? null; // 路径上的参数，如果没有，则为null
        return array_merge($args, [
            'metadata' => $metadata
        ]);
    }

    public  function index3()
    {

    }
}
